// HQHackMac.m
//
// You may freely copy, distribute, and reuse the code in this example.
// Apple disclaims any warranty of any kind, expressed or  implied, as to its
// fitness for any particular use.

#import "HQHackMac.h"
#import "HQInfoPanelController.h"
#import "HQPreferencesController.h"
#import "HQApplication.h"
#import <stdlib.h>

BOOL gOn = false;

// If you want the bundle to insert a menu into apps that load it, uncomment the next line.  Otherwise, comment it out.
#define ENABLE_MENU

@implementation HQHackMac

// ************************* Set up *************************

+ (void)load {
    [HQApplication doNSAppInstancePose];
    srand(time(NULL));
#ifdef ENABLE_MENU
    // Arrange to install the menu.  We will try at least three different ways of getting notified.
    // We try to install the menu at willFinishLaunching, but we will try again at didFinishLaunching just in case willFinishLaunching passed us by.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationWillFinishLaunchingNotification object:NSApp];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationDidFinishLaunchingNotification object:NSApp];
    // Finally, there may be cases where the didFinishLaunching has already happened by the time we get loaded.  So we'll also try it with a timer too.
    [self performSelector:@selector(tryToInstallMenu:) withObject:nil afterDelay:0.0];
#endif
}

// ********************** Install extras menu **********************

+ (void)tryToInstallMenu:(NSNotification *)notification {
    static BOOL alreadyInstalled = NO;
    NSMenu *mainMenu = nil;

    // We only try once, but that one time needs to be after there is a main menu.  If there is none now, we will pass and hope this method gets called again later.
    if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
        NSMenu *insertIntoMenu = nil;
        NSMenuItem *item;
        unsigned insertLoc = NSNotFound;
        NSBundle *bundle = [NSBundle bundleForClass:[HQHackMac class]];
        // Succeed or fail, we do not try again.
        alreadyInstalled = YES;

        item = (([mainMenu numberOfItems] > 0) ? [mainMenu itemAtIndex:0] : nil);
        if (item && [item hasSubmenu]) {
            insertIntoMenu = [item submenu];
            item = [insertIntoMenu itemWithTitle:NSLocalizedStringFromTableInBundle(@"Services", @"Localizable", bundle, @"Title of Services menu item")];
            if (!item) {
                // MF: Try again with unlocalized string "Text".  This is a backstop that assumes that any app not localized into the user's chosen language was probably developed in English...
                item = [insertIntoMenu itemWithTitle:@"Services"];
            }
            if (item) {
                insertLoc = [[insertIntoMenu itemArray] indexOfObjectIdenticalTo:item] + 1;
            } else {
                // Just insert at head of menu.  This is kind of yucky...
                insertLoc = 0;
            }
        } 

        if (insertIntoMenu) {
            item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"BOFH - OFF", @"Localizable", bundle, @"Title of bofh menu") action:@selector(runBOFH:) keyEquivalent:@"" atIndex:insertLoc];
            [item setTarget:self];
        }
    }
}

+ (void)runBOFH:(id)sender {
    if (gOn) [sender setTitle:@"BOFH - OFF"];
    else [sender setTitle:@"BOFH - ON"];
    gOn = !gOn;
}

+ (BOOL)validateMenuItem:(NSMenuItem *)item {
    if ([item action] == @selector(leetAction:)) {
        [item setState:gOn];
    }
    return YES;
}

@end
